﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Telerik.Web.UI;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Globalization;

namespace BVVD_Website.ql_thi_sinh
{
    public partial class frmThisinh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvKythi.Culture = new CultureInfo("vi-VN");
                grvKythi.Rebind();

                BVVDDataContext db = new BVVDDataContext();
                List<LST_KYTHI> list = (from kt in db.LST_KYTHIs
                                        where kt.TRANGTHAI.Equals("1")
                                        select kt).ToList();
                ddlKYTHI.DataSource = list;
                ddlKYTHI.DataValueField = "MAKT";
                ddlKYTHI.DataTextField = "TENKT";
                ddlKYTHI.DataBind();
                fillgrvThisinh();
                grvKythi.DataBind();

            }
        }

        protected void ddlKYTHI_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvThisinh();
            grvKythi.DataBind();
        }

        protected void grvKythi_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvThisinh();
        }

        protected void grvKythi_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            BVVDDataStore<LST_DANHMUC> type = new BVVDDataStore<LST_DANHMUC>();
            
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;
                LinkButton btnUpdate = editFormItem.FindControl("btnUpdate") as LinkButton;
                Label lblMAKT = editFormItem.FindControl("lblMAKT") as Label;

                RadDropDownList ddlQUOCTICH = editFormItem.FindControl("ddlQUOCTICH") as RadDropDownList;
                Hashtable quoctich = new Hashtable();
                quoctich.Add("MALOAIDM", "QUOCGIA");
                quoctich.Add("TRANGTHAI", "1");
                ddlQUOCTICH.DataSource = type.getFromDB(quoctich);
                ddlQUOCTICH.DataTextField = "TENDM";
                ddlQUOCTICH.DataValueField = "MADM";


                RadDropDownList ddlDANTOC = editFormItem.FindControl("ddlDANTOC") as RadDropDownList;
                Hashtable dantoc = new Hashtable();
                dantoc.Add("MALOAIDM", "DANTOC");
                dantoc.Add("TRANGTHAI", "1");
                ddlDANTOC.DataSource = type.getFromDB(dantoc);
                ddlDANTOC.DataTextField = "TENDM";
                ddlDANTOC.DataValueField = "MADM";         

                
                RadDropDownList ddlGIOITINH = editFormItem.FindControl("ddlGIOITINH") as RadDropDownList;
                Hashtable gioitinh = new Hashtable();
                gioitinh.Add("MALOAIDM", "GIOITINH");
                gioitinh.Add("TRANGTHAI", "1");
                ddlGIOITINH.DataSource = type.getFromDB(gioitinh);
                ddlGIOITINH.DataTextField = "TENDM";
                ddlGIOITINH.DataValueField = "MADM";

                
                RadioButtonList rblLOAIGIAY = editFormItem.FindControl("rblLOAIGIAY") as RadioButtonList;                
                RadioButtonList rblTHUONGBINH = editFormItem.FindControl("rblTHUONGBINH") as RadioButtonList;
                RadioButtonList rblLIETSY = editFormItem.FindControl("rblLIETSY") as RadioButtonList;
                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                RadBinaryImage rbiPICTURE = editFormItem.FindControl("rbiPICTURE") as RadBinaryImage;

                RadTextBox txtMAHV = editFormItem.FindControl("txtMAHV") as RadTextBox;




                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    if (ddlKYTHI.SelectedIndex >= 0)
                    {
                        lblMAKT.Text = "KỲ THI "  + ddlKYTHI.SelectedText.ToUpper();
                        txtMAHV.ReadOnly = false;
                    }
                    else {
                        lblMAKT.Text = "<a href='frmThisinh.aspx' style='color:red' >QUAY LẠI ĐỂ CHỌN KỲ THI</aa>";
                        txtMAHV.ReadOnly = true;
                        btnUpdate.Enabled = false;
                    
                    }
                }
                else
                {
                    ddlQUOCTICH.SelectedValue = editFormItem["QUOCGIA"].Text;
                    ddlDANTOC.SelectedValue = editFormItem["DANTOC"].Text;                    
                    ddlGIOITINH.SelectedValue = editFormItem["GIOITINH"].Text;                    
                    rblLOAIGIAY.SelectedValue = editFormItem["LOAIGIAY"].Text;
                    rblTHUONGBINH.SelectedValue = editFormItem["THUONGBINH"].Text;
                    rblLIETSY.SelectedValue = editFormItem["CONLIETSY"].Text;
                    Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    rblTRANGTHAI.SelectedValue = lblTRANGTHAI.Text.Equals("ACTIVE") ? "1" : "0";
                    txtMAHV.ReadOnly = true;
                    txtMAHV.BackColor = Color.WhiteSmoke;

                }
                ddlQUOCTICH.DataBind();
                ddlDANTOC.DataBind();
                
                ddlGIOITINH.DataBind();


            }
        }

        protected void grvKythi_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            RadAsyncUpload rauUpload = insertedItem.FindControl("rauUpload") as RadAsyncUpload;

            LST_THISINH ts = new LST_THISINH
            {
                MATS = (insertedItem.FindControl("txtMAHV") as RadTextBox).Text,
                HODEM = (insertedItem.FindControl("txtHODEM") as RadTextBox).Text,
                TENTS = (insertedItem.FindControl("txtTENHV") as RadTextBox).Text,
                MAKT = ddlKYTHI.SelectedValue,
                NGAYSINH = (insertedItem.FindControl("rdpNGAYSINH") as RadDatePicker).SelectedDate,
                GIOITINH = (insertedItem.FindControl("ddlGIOITINH") as RadDropDownList).SelectedValue,
                LOAIGIAY = Int32.Parse((insertedItem.FindControl("rblLOAIGIAY") as RadioButtonList).SelectedValue),
                SOCMND = (insertedItem.FindControl("rntxtCMND") as RadTextBox).Text,
                NGAYCAP = (insertedItem.FindControl("rdpNGAYCAP") as RadDatePicker).SelectedDate,
                NOICAP = (insertedItem.FindControl("txtNOICAP") as RadTextBox).Text,
                DIACHI = (insertedItem.FindControl("txtDIACHI") as RadTextBox).Text,                
                TEL = (insertedItem.FindControl("txtTEL") as RadTextBox).Text,
                MOBILE = (insertedItem.FindControl("txtMOBILE") as RadTextBox).Text,
                EMAIL = (insertedItem.FindControl("txtEMAIL") as RadTextBox).Text,
                QUOCTICH = (insertedItem.FindControl("ddlQUOCTICH") as RadDropDownList).SelectedValue,
                DANTOC = (insertedItem.FindControl("ddlDANTOC") as RadDropDownList).SelectedValue,                
                THUONGBINH = Int32.Parse((insertedItem.FindControl("rblTHUONGBINH") as RadioButtonList).SelectedValue),
                CONLIETSY = Int32.Parse((insertedItem.FindControl("rblLIETSY") as RadioButtonList).SelectedValue),
                
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text

            };
            if (rauUpload.UploadedFiles != null)
            {
                try
                {
                    UploadedFile file = rauUpload.UploadedFiles[0];
                    byte[] fileData = new byte[file.InputStream.Length];
                    file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
                    ts.PICTURE = fileData;
                }
                catch {
                
                }

            } 

            BVVDDataContext db = new BVVDDataContext();
            db.LST_THISINHs.InsertOnSubmit(ts);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới thí sinh có mã <font color='red'> " + ts.MATS + "</font> vào kỳ thi <font color='red'> " +ts.LST_KYTHI.TENKT + "</font> thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n <br /> Nguyên nhân : " + ex.Message;

            }


            rnAlert.Show();
        }

        protected void grvKythi_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string mats = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["MATS"].ToString();

            LST_THISINH ts = (from hv in db.LST_THISINHs
                                   where hv.MATS.Equals(mats)
                                   select hv).SingleOrDefault();
            RadAsyncUpload rauUpload = editedItem.FindControl("rauUpload") as RadAsyncUpload;



            ts.HODEM = (editedItem.FindControl("txtHODEM") as RadTextBox).Text;
            ts.TENTS = (editedItem.FindControl("txtTENHV") as RadTextBox).Text;
            ts.NGAYSINH = (editedItem.FindControl("rdpNGAYSINH") as RadDatePicker).SelectedDate;
            ts.GIOITINH = (editedItem.FindControl("ddlGIOITINH") as RadDropDownList).SelectedValue;
            ts.LOAIGIAY = Int32.Parse((editedItem.FindControl("rblLOAIGIAY") as RadioButtonList).SelectedValue);
            ts.SOCMND = (editedItem.FindControl("rntxtCMND") as RadTextBox).Text;
            ts.NGAYCAP = (editedItem.FindControl("rdpNGAYCAP") as RadDatePicker).SelectedDate;
            ts.NOICAP = (editedItem.FindControl("txtNOICAP") as RadTextBox).Text;
            ts.DIACHI = (editedItem.FindControl("txtDIACHI") as RadTextBox).Text;            
            ts.TEL = (editedItem.FindControl("txtTEL") as RadTextBox).Text;
            ts.MOBILE = (editedItem.FindControl("txtMOBILE") as RadTextBox).Text;
            ts.EMAIL = (editedItem.FindControl("txtEMAIL") as RadTextBox).Text;
            ts.QUOCTICH = (editedItem.FindControl("ddlQUOCTICH") as RadDropDownList).SelectedValue;
            ts.DANTOC = (editedItem.FindControl("ddlDANTOC") as RadDropDownList).SelectedValue;            
            ts.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            ts.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            try
            {
                UploadedFile file = rauUpload.UploadedFiles[0];
                byte[] fileData = new byte[file.InputStream.Length];
                file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
                ts.PICTURE = fileData;
            }
            catch
            {

            }

            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công thí sinh có mã số <font color='red'>  " + ts.MATS + "</font> trong kỳ thi <font color='red'> " + ts.LST_KYTHI.TENKT + "</font>";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvKythi_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string mats = item.OwnerTableView.DataKeyValues[item.ItemIndex]["MATS"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_THISINH ts = (from hv in db.LST_THISINHs
                                   where hv.MATS.Equals(mats)
                                   select hv).SingleOrDefault();
            string rnText = "Xóa thành công thí sinh có mã  <font color='red'> " + ts.MATS + "</font> ra khỏi danh sách thí sinh trong kỳ thi <font color='red'>" + ts.LST_KYTHI.TENKT +"</font>";
            db.LST_THISINHs.DeleteOnSubmit(ts);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = rnText;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n <br /> Nguyên nhân : " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvKythi_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                e.Canceled = true;
                System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                newValues["PICTURE"] = File.ReadAllBytes(Server.MapPath("/images/no_image.jpg"));
                newValues["MATS"] = String.Empty;
                newValues["HODEM"] = String.Empty;
                newValues["TENTS"] = String.Empty;
                newValues["NGAYSINH"] = String.Empty;
                newValues["NGAYCAP"] = String.Empty;
                newValues["TENTS"] = String.Empty;
                newValues["SOCMND"] = String.Empty;
                newValues["NOICAP"] = String.Empty;
                newValues["DIACHI"] = String.Empty;
                newValues["TEL"] = String.Empty;
                newValues["MOBILE"] = String.Empty;
                newValues["EMAIL"] = String.Empty;                
                newValues["GHICHU"] = String.Empty;
                e.Item.OwnerTableView.InsertItem(newValues);
            }
        }

        private void fillgrvThisinh()
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from ts in db.LST_THISINHs
                        select new
                        {
                            RECID = ts.RECID,
                            MAKT = ts.MAKT,
                            MATS = ts.MATS,
                            HODEM = ts.HODEM,
                            TENTS = ts.TENTS,
                            FULLNAME = ts.HODEM + " " + ts.TENTS,
                            NGAYSINH = ts.NGAYSINH,
                            GIOITINH = ts.GIOITINH,
                            LOAIGIAY = ts.LOAIGIAY,
                            SOCMND = ts.SOCMND,
                            NGAYCAP = ts.NGAYCAP,
                            NOICAP = ts.NOICAP,
                            DIACHI = ts.DIACHI,
                            TEL = ts.TEL,
                            MOBILE = ts.MOBILE,
                            EMAIL = ts.EMAIL,
                            QUOCTICH = ts.QUOCTICH,
                            DANTOC = ts.DANTOC,
                            THUONGBINH = ts.THUONGBINH,
                            CONLIETSY = ts.CONLIETSY,
                            PICTURE = ts.PICTURE,
                            TRANGTHAI = ts.TRANGTHAI,
                            GHICHU = ts.GHICHU
                        }).ToList().Where(a => a.MAKT.Equals(ddlKYTHI.SelectedValue));

            grvKythi.DataSource = list;


        }
    }
}